package zj.xinxin.business.business.articale;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import zj.xinxin.api.moudle.article.dto.DiscussDto;
import zj.xinxin.api.moudle.article.entity.DiscussEntity;
import zj.xinxin.api.moudle.article.api.DiscussService;
import zj.xinxin.api.moudle.article.vo.DiscussVo;
import zj.xinxin.business.business.articale.converter.DiscussConverter;
import zj.xinxin.mapper.mapper.article.DiscussMapper;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 评论  服务实现类
 * </p>
 *
 * @author zhoujun
 * @since 2022-04-12
 */
@Service
public class DiscussServiceImpl extends ServiceImpl<DiscussMapper, DiscussEntity> implements DiscussService {
    @Override
    public Boolean saveDiscuss(DiscussVo discuss) {
        return this.save(DiscussConverter.INSTANCE.toEntity(discuss));
    }

    @Override
    public List<DiscussDto> getByArticleId(Long articleId) {

        if (Objects.isNull(articleId) || articleId <= 0L){
            return Collections.emptyList();
        }
        List<DiscussEntity> discussList = this.baseMapper.getByArticleId(articleId);
        if (CollectionUtils.isEmpty(discussList)){
            return Collections.emptyList();
        }
        return discussList.stream()
                .map(DiscussConverter.INSTANCE::toDto)
                .collect(Collectors.toList());
    }
}
